package online.afeibaili.storebackendframework.controller.util

import online.afeibaili.storebackendframework.model.CouponEntity
import online.afeibaili.storebackendframework.model.json.CouponJsonMapper


/**
 * 优惠券逻辑工具类
 *
 *@author AfeiBaili
 *@version 2025/10/28 13:04
 */

object CouponUtil {
    fun CouponJsonMapper.findByList(list: List<CouponEntity>) = list.find { it.createTime == this.couponId }

    fun CouponEntity.verify(couponJsonMapper: CouponJsonMapper): Pair<Boolean, String> {
        if (couponJsonMapper.couponId != this.createTime) return false to "未知的id"
        val currentTimeMillis: Long = System.currentTimeMillis()
        val expireTime: Long = couponJsonMapper.getTime + this.expireTime
        if (currentTimeMillis - expireTime < 0) return false to "当前优惠券已过期"
        if (couponJsonMapper.count < 1) return false to "优惠券使用了0张"

        return true to "ok"
    }
}